//
//  KLRegisterDetailViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/4/24.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLRegisterDetailViewController.h"
#import "HemaConst.h"
#import "KLHttpAPI.h"
#import "KLHelp.h"
#import "JRToast.h"

@interface KLRegisterDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UITextField *passwordAgainField;

@property (nonatomic,strong) UIButton *registerBtn;

@end

@implementation KLRegisterDetailViewController

//懒加载
-(UIButton *)registerBtn{
    if(!_registerBtn){
        _registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, self.passwordAgainField.frame.origin.y +50, UI_View_Width-60, 40)];
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registerBtn setBackgroundColor:[KLHelp mainBlueColor]];
        _registerBtn.layer.cornerRadius = 5;
    }
    
    return _registerBtn;
}

-(void)viewWillAppear:(BOOL)animated{
    self.navigationItem.title = @"注册详情";
    
    //左侧返回按钮
    UIButton *backbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
    [backbutton setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backbutton];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.registerBtn addTarget:self action:@selector(regist:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registerBtn];
}

#pragma mark -方法中的方法体

-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

//点击注册按钮
-(void)regist:(UIButton *)sender{
    [self.view endEditing:YES];
    
    NSString *userName = self.userNameField.text;
    if(userName.length == 0){
        [JRToast showWithText:@"用户名不能为空" bottomOffset:50 duration:1.5];
        return;
    }else if(self.passwordField.text.length == 0){
        [JRToast showWithText:@"请输入密码" bottomOffset:50 duration:1.5];
        return;
    }
    
    if(self.passwordField.text.length < 6){
        [JRToast showWithText:@"密码不能低于6位" bottomOffset:50 duration:1.5];
        return;
    }else if(![self.passwordField.text isEqualToString:self.passwordAgainField.text]){
        [JRToast showWithText:@"两次输入密码不一致" bottomOffset:50 duration:1.5];
        return;
    }else{
        NSString *temp_token = [[NSUserDefaults standardUserDefaults]objectForKey:@"temp_token"];;
        NSLog(@"传入的临时令牌:%@",temp_token);
        [KLHelp showHUDAddedTo:self.view title:@"请稍候..." details:nil animated:YES];
        [KLHttpAPI requestRegisterResultWithUserName:self.userNameField.text password:self.passwordField.text phoneNumber:self.phoneNumber temp_token:temp_token completionHandler:^(NSString *responseString) {
            [KLHelp hideAllHUDsForView:self.view animated:YES];
            
            NSDictionary *responseDic = [KLHelp dictionaryWithJsonString:responseString];
            NSString *isTrue = [responseDic objectForKey:@"success"];
            if(isTrue.integerValue == 1){
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isAutoLogin"];
                //将正式令牌传入底层
                NSArray *inforArr = [responseDic objectForKey:@"infor"];
                NSString *token = [inforArr[0] objectForKey:@"token"];
                [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"token"];
                
                //注册成功将用户信息存入底层
                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                [userDefault setObject:self.userNameField.text forKey:@"userName"];
                [userDefault setObject:self.phoneNumber forKey:@"phoneNumber"];
                [userDefault setObject:self.passwordField.text forKey:@"password"];
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                [JRToast showWithText:@"注册成功" bottomOffset:50 duration:1.5];
                
            }else{
                [JRToast showWithText:[responseDic objectForKey:@"msg"] bottomOffset:UI_View_Height/2 duration:1.5];
            }
            
            NSLog(@"responseTring:%@",responseString);
            
        } errorHandler:^(NSString *error) {
            [KLHelp hideAllHUDsForView:self.view animated:YES];
            [JRToast showWithText:error];
        }];
    }
}

#pragma mark -取消view的第一响应者身份
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
